﻿Imports System.Data.SqlClient
Imports System.Net

Public Class orders
    Public Serial As String
    Public dKey As String
    Public Status As String
    Public Guestname As String
    Public GuestAddress As String
    Public ContactNo As String
    Public order_ As String
    Public totalBill As String
    Public ChangeFor As String
    Public DateSent As String
    Public specialIns As String
    Public sender_ As String
    Public DateModified As String

    Public mainform As mainform
    Private Sub orders_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False

        If mainform.AccountType = 2 Or mainform.AccountType = 0 Then
            Timer1.Enabled = True
            Timer1.Start()
            'Me.BackColor = Color.WhiteSmoke

            If Status = "OPEN" Then
                Me.Font = New Font("Tahoma", 8, FontStyle.Bold)
                'Blink
                'Me.BackColor = Color.WhiteSmoke
            ElseIf Status = "READ" Then
                Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
            Else
                ClosedOrder()
            End If
        Else
            StopBlinking()
        End If
        lblGuest.Text = Guestname
        txtBill.Text = Serial
        txtDate.Text = DateSent
        Label1.Text = sender_
    End Sub
    Sub StopBlinking()
        Timer1.Enabled = False
        Timer1.Stop()
        Me.BackColor = Color.WhiteSmoke
    End Sub
    Sub ClosedOrder()
        Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
        Me.ForeColor = Color.Gray
        'Me.lblGuest.Font = New Font(lblGuest.Font, FontStyle.Strikeout)

        For Each Label_ As Control In Me.Controls
            If Label_.GetType Is GetType(Label) Then
                Label_.Font = New Font(Label_.Font, FontStyle.Strikeout)
            End If
        Next

        Status = "CLOSE"
        OrderDialog.Status = "CLOSE"
    End Sub
    Sub ShowOrder(credential As Integer)
        Try
            Dim orderdialog As New OrderDialog
            orderdialog.Serial = Serial
            orderdialog.dKey = dKey
            orderdialog.Status = Status
            orderdialog.Guestname = Guestname
            orderdialog.GuestAddress = GuestAddress
            orderdialog.ContactNo = ContactNo
            orderdialog.order_ = order_
            orderdialog.totalBill = totalBill
            orderdialog.ChangeFor = ChangeFor
            orderdialog.DateSent = DateSent
            orderdialog.specialIns = specialIns
            orderdialog.Sender_ = sender_
            orderdialog.Credential = credential
            orderdialog.DateModified = DateModified
            If orderdialog.ShowDialog() = DialogResult.OK Then
                If mainform.IncludeClose = False Then
                    Me.Dispose()
                Else
                    ClosedOrder()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub TagRead()
        Try
            Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
            'Me.Width = 350

            If Status = "OPEN" Then
                If mainform.Mode = 1 Then
                    Try
                        Dim myConn As SqlConnection
                        Dim myCmd As SqlCommand
                        myConn = New SqlConnection(mainform.ConnectionString)
                        myCmd = myConn.CreateCommand
                        myCmd.CommandText = "UPDATE " & mainform.DBTable & " SET status = 'READ' where dKey=" & dKey

                        myConn.Open()
                        myCmd.ExecuteNonQuery()
                        Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
                        ''Me.Width = 350
                    Catch ex As Exception

                    End Try
                ElseIf mainform.Mode = 2 Then
                    Dim myQuery As String = "mark=READ&dKey=" & dKey
                    Dim Webreq As HttpWebRequest = DirectCast(WebRequest.Create(mainform.Host & ":" & mainform.port & "/" & mainform.MarkPage & "?" & myQuery), HttpWebRequest)
                    Webreq.Method = "GET"
                    Webreq.KeepAlive = False

                    Dim postresponse As HttpWebResponse
                    postresponse = DirectCast(Webreq.GetResponse(), HttpWebResponse)

                    If postresponse.StatusCode = HttpStatusCode.OK Then
                        Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
                        ''Me.Width = 350
                    End If
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Button1.Text = "Open" Then
            StopBlinking()
            'Set in Database as READ
            Dim t As New Threading.Thread(AddressOf TagRead)
            t.IsBackground = True
            t.Start()
            ShowOrder(1)
        ElseIf Button1.Text = "Preview" Then
            ShowOrder(0)
        End If
    End Sub

    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Status = "OPEN" Then
            If Me.BackColor = Color.WhiteSmoke Then
                Me.BackColor = Color.LightPink
            Else
                Me.BackColor = Color.WhiteSmoke
            End If
        ElseIf Status = "READ" Then
            Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
            Me.BackColor = Color.WhiteSmoke
        Else
            Me.Font = New Font("Tahoma", 8, FontStyle.Regular)
            Me.ForeColor = Color.Gray
            Me.BackColor = Color.WhiteSmoke
        End If
    End Sub
End Class
